package cn.com.dhc.danlu.activity.delivery;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.widget.DLToolbar;
import cn.com.dhc.danlu.widget.base.BasePopupWindow;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2016-10-10 15:59<br/>
 *
 * <p>
 * 订单使用经销商优惠券
 * </p>
 */
public class OrderDealerEnvelopPopView extends BasePopupWindow {
    private RelativeLayout rlUse;
    private RelativeLayout rlNotUse;

    private View.OnClickListener onChooseListener;

    /**
     * 提交订单界面,选择经销商红包的弹出窗
     *
     * @param envelop
     *         经销商红包信息,如:"满减999元"
     */
    public OrderDealerEnvelopPopView(Context context, ViewGroup rootView, String envelop) {
        super(context);
        init(rootView, envelop);
    }

    private void init(ViewGroup rootView, String envelop) {
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setAnimationStyle(R.style.popwin_anim_style);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setOutsideTouchable(true);

        Context context = rootView.getContext();
        View popView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.popview_order_dealer_envelop, rootView, false);
        setFocusable(true);
        initChildView(popView, envelop);
        setContentView(popView);
    }

    private void initChildView(View popView, String envelop) {
        TextView tvUse = (TextView) popView.findViewById(R.id.tv_use);
        rlUse = (RelativeLayout) popView.findViewById(R.id.rl_use);
        rlNotUse = (RelativeLayout) popView.findViewById(R.id.rl_not_use);
        DLToolbar dlToolbar = (DLToolbar) popView.findViewById(R.id.dl_toolbar);

        tvUse.setText(envelop);

        rlUse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectState(true, false);
                onChooseListener.onClick(v);
                dismiss();
            }
        });

        rlNotUse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectState(false, true);
                onChooseListener.onClick(v);
                dismiss();
            }
        });

        dlToolbar.setRightClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setSelectState(boolean isUsed, boolean isNotUsed) {
        rlUse.findViewById(R.id.iv_use).setSelected(isUsed);
        rlNotUse.findViewById(R.id.iv_not_use).setSelected(isNotUsed);
    }

    public void setOnChooseListener(View.OnClickListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }
}